<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Jobs extends MY_Controller {

    protected $lang;

    public function __construct() {

        parent::__construct();


        $this->data['menu_config'] = $this->menu_config_7;

        $user_session = $this->session->userdata('user');
        $this->data['user_session'] = $user_session;
        if (empty($user_session)) {
            redirect(admin_url('authenticate/login'));
        }

        $this->load_lang('admincp/jobs');
        $this->lang = $this->lang->language;
        $this->data['lang'] = $this->lang;
        $this->load->model('jobs_model', 'jobs');
        $this->permission();
    }

    public function index() {
        $this->load_view('jobs/index', 'admincp');
    }

    public function lists($start = 0) {
        $posts = $this->input->post();

        $dataWhere = array();
        if ($posts['status'] != 'all')
            $dataWhere['jobs.status'] = $posts['status'];
        if (!empty($posts['search']))
            $dataWhere['search'] = $posts['search'];
        $limit = $this->configs['record_per_page'];
        if (!empty($posts['asc'])) {
            $sort[$posts['sort']] = 'ASC';
        } else {
            $sort[$posts['sort']] = 'DESC';
        }
//       Begin pagination
        $this->load->library("pagination");
        $config = array();
        $config["total_rows"] = $this->jobs->totalJobs($dataWhere);
        $config["base_url"] = admin_url('job/list');
        $config["per_page"] = $limit;
        $page = $start;
        $config["uri_segment"] = 3;
        $config['num_links'] = 2;

        $config['first_link'] = $this->lang['first_link'];
        $config['first_tag_open'] = '<div class="nav-button">';
        $config['first_tag_close'] = '</div>';
        $config['last_link'] = $this->lang['last_link'];
        $config['last_tag_open'] = '<div class="nav-button">';
        $config['last_tag_close'] = '</div>';
        $config['cur_tag_open'] = "<div class='nav-button'><div class='nav-page nav-page-selected'>";
        $config['cur_tag_close'] = '</div></div>';
        $config['num_tag_open'] = "<div class='nav-button'><div class='nav-page'>";
        $config['num_tag_close'] = '</div></div>';
        $config['prev_tag_open'] = "<div class='nav-button'>";
        $config['prev_link'] = $this->lang['prev_link'];
        $config['prev_tag_close'] = '</div>';
        $config['next_link'] = $this->lang['next_link'];
        $config['next_tag_open'] = "<div class='nav-button'>";
        $config['next_tag_close'] = '</div>';
        $this->pagination->initialize($config);
        $json["links"] = $this->pagination->create_links();
//       End pagination

        $this->data['jobs'] = $this->jobs->getJobs($dataWhere, $limit, $start, $sort);
        $json['jobs'] = $this->load->view('admincp/jobs/list', $this->data, true);
        echo json_encode($json);
    }

    public function form($id = 0) {
        $job = $this->jobs->getJobById($id);
        if ($job) {
            $this->data['job'] = $job;
        }

        $posts = $this->input->post();

        if ($posts) {
            foreach ($posts as $key => $post) {
                $job->$key = $post;
            }
//            $job = $jobData;
//            var_dump($job);die;
            $this->data['job'] = $job;

            if ($this->validate($job)) {
                unset($posts['repassword']);
                if (empty($job->job_id)) {
                    $job_id = $this->jobs->insertJob($posts);
                    redirect(admin_url('jobs'));
                    $this->session->set_flashdata('success', $this->lang['add_success']);
                } else {
                   
                    $this->jobs->updateJobs($job->job_id, $posts);
                    redirect(admin_url('jobs'));
                    $this->session->set_flashdata('success', $this->lang['edit_success']);
                }
            }
        }
        $this->html_form($job);
        $this->load_view('jobs/form', 'admincp');
    }

    private function validate($job) {
        $posts = $this->input->post();
        $flag = true;

        if (!$this->hasPermission('modify')) {
            $this->data['error'][] = $this->lang['error_permission_modify'];
            $flag = false;
        }

        foreach ($posts['languages'] as $language) {
            if (strlen(trim($language['name'])) == 0){
                $this->data['error'][] = $this->lang['error_name'];
                $flag = false;
            }
                
        }


        return $flag;
    }

    public function active($id = 0) {
        $this->job->update($id, array('status' => 1));
    }

    public function deactive($id = 0) {
        $this->job->update($id, array('status' => 0));
    }

    private function html_form($job) {
//        $languages = 
        $list_languages = array();
        if(!empty($job) && empty($job->job_id)){
            $data_languages = $job->languages;
            foreach ($data_languages as $lg_id => $description){
                $language_val = null;
                foreach ($description as $key => $val){
                    $language_val->$key = $val;
                }
                $list_languages[$lg_id] = $language_val;
            }
        }elseif (!empty($job->job_id)) {
            $job_descriptions = $this->jobs->getJobDescriptions($job->job_id);
            foreach ($job_descriptions as $descriptions) {
                $list_languages[$descriptions->language_id] = $descriptions;
            }
        }
        $languages = $this->languages->getLanguages(array('status' => 1));
        foreach ($languages as $language) {
            $this->data['language_tabs'][$language->language_id] = $language->name;
            $this->data['language_descriptions'][$language->language_id] = array(
                array(
                    'type' => 'input',
                    'label' => $this->lang['entry_name'],
                    'require' => true,
                    'value' => array(
                        'name' => 'languages[' . $language->language_id . '][name]',
                        'id' => 'name',
                        'class' => 'st-forminput',
                        'value' => !empty($list_languages[$language->language_id]->name) ? $list_languages[$language->language_id]->name : ''
                    )
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->lang['entry_description'],
                    'value' => array(
                        'name' => 'languages[' . $language->language_id . '][description]',
                        'id' => 'description',
                        'class' => 'st-forminput',
                        'style' => 'width:500px',
                        'value' => !empty($list_languages[$language->language_id]->description) ? $list_languages[$language->language_id]->description : ''
                    )
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->lang['entry_sort_description'],
                    'value' => array(
                        'name' => 'languages[' . $language->language_id . '][sort_description]',
                        'id' => 'sort_description',
                        'class' => 'st-forminput',
                        'style' => 'width:500px',
                        'value' => !empty($list_languages[$language->language_id]->sort_description) ? $list_languages[$language->language_id]->sort_description : ''
                    )
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->lang['entry_meta_description'],
                    'value' => array(
                        'name' => 'languages[' . $language->language_id . '][meta_description]',
                        'id' => 'meta_description',
                        'class' => 'st-forminput',
                        'style' => 'width:500px',
                        'value' => !empty($list_languages[$language->language_id]->meta_description) ? $list_languages[$language->language_id]->meta_description : ''
                    )
                ),
                array(
                    'type' => 'input',
                    'label' => $this->lang['entry_meta_keyword'],
                    'value' => array(
                        'name' => 'languages[' . $language->language_id . '][meta_keyword]',
                        'id' => 'meta_keyword',
                        'class' => 'st-forminput',
                        'value' => !empty($list_languages[$language->language_id]->meta_keyword) ? $list_languages[$language->language_id]->meta_keyword : ''
                    )
                ),
                array(
                    'type' => 'input',
                    'label' => $this->lang['entry_tag'],
                    'value' => array(
                        'name' => 'languages[' . $language->language_id . '][tag]',
                        'id' => 'tag',
                        'class' => 'st-forminput',
                        'value' => !empty($list_languages[$language->language_id]->tag) ? $list_languages[$language->language_id]->tag : ''
                    )
                ),
            );
        }
        $this->data['form'] = array(
            array(
                'type' => 'input',
                'label' => $this->lang['entry_sort_order'],
                'value' => array(
                    'name' => 'sort_order',
                    'id' => 'sort_order',
                    'class' => 'st-forminput',
                    'value' => !empty($job->sort_order) ? $job->sort_order : ''
                )
            )
        );

        $this->data['input_submit'] = array(
            'value' => $this->lang['btn_save'],
            'class' => 'button-blue'
        );
    }

}